﻿/*
 * @Author: ws.s 
 * @Date: 2021-06-10 18:30:51 
 * @Last Modified by: ws.s
 * @Last Modified time: 2021-06-10 18:31:47
 */
using System.Numerics;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Text.RegularExpressions;

namespace WssUtils
{
    /// <summary>
    /// 修复 KMBT_FIXED2 的正则表达式
    /// </summary>
    public static class StringTool
    {
        public enum VALUE_TYPE
        {
            INTEGER,
            FIXED_2,
            TIMER,
            TIMER_FULL,
            PERCENTAGE,
            KMBT_FIXED2,
            THOUSAND_SEPARATOR,
            FIXED_1,
            CUSTOMER
        }

        public static string ParseTimer(float timer = 0, bool isFullTimer = true)
        {
            var t = timer;
            var hours = Mathf.FloorToInt(t / 3600);
            var mins = Mathf.FloorToInt((t % 3600) / 60);
            var secs = Mathf.FloorToInt(t % 60);
            string m = "" + mins;
            string s = "" + secs;
            if (secs < 10) s = "0" + secs;

            //full timer 按小时算,无论有没有小时
            if (isFullTimer)
            {
                if (mins < 10) m = "0" + mins;
                return hours + ':' + m + ':' + s;
            }
            else
            {
                m = "" + (mins + hours * 60);
                if (mins < 10) m = "0" + mins;
                return m + ':' + s;
            }
        }

        static Regex _regex_KMBT_FIXED2 = new Regex("E.*?0");

        /// <summary>
        /// 获取排名后缀，比如 第一名 是 1st ，第2名是 2nd
        /// </summary>
        /// <returns></returns>
        public static string GetRankStringSuffix(int rank)
        {
            if (rank <= 1)
                return "st";
            else if (rank <= 2)
                return "nd";
            else if (rank <= 3)
                return "rd";
            else
                return "th";
        }

        /// <summary>
        /// 将较大数 double 转换为 KMBT E1~E100 的 格式
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static string FormatKMBT(double _value, string format = "F2", bool showSymbol = false)
        {
            string str = "";

            double value = System.Math.Abs(_value);
            if (value >= double.MaxValue)
            {
                str = "MAX";
            }
            else if (value >= 1000000000000000)
            {
                str = _regex_KMBT_FIXED2.Replace((_value).ToString("E1"), "E");
            }
            else if (value >= 1000000000000)
            {
                str = (_value / 1000000000000).ToString(format) + 'T';
            }
            else if (value >= 1000000000)
            {
                str = (_value / 1000000000).ToString(format) + 'B';
            }
            else if (value >= 1000000)
            {
                str = (_value / 1000000).ToString(format) + 'M';
            }
            else if (value >= 9999)
            {
                str = (_value / 1000).ToString(format) + "K";
            }
            else
            {
                str = System.Math.Round(_value).ToString();
            }

        
            if (showSymbol)
            {
                if (_value > 0)
                {
                    str = "+" + str;
                }
            }
            return str;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="value">数值</param>
        /// <param name="type">格式化类型</param>
        /// <param name="showSymbol">是否显示加号</param>
        /// <returns></returns>
        public static string Format(float value, VALUE_TYPE type, bool showSymbol = false)
        {
            string str = "";
            switch (type)
            {
                case VALUE_TYPE.INTEGER://最终显示整数类型
                    str = Mathf.Round(value) + "";
                    break;
                case VALUE_TYPE.FIXED_2://最终显示两位小数类型
                    str = value.ToString("F2");
                    break;
                case VALUE_TYPE.FIXED_1://最终显示1位小数类型
                    str = value.ToString("F1");
                    break;
                case VALUE_TYPE.TIMER: //最终显示 计时器类型
                    str = ParseTimer(value, false);
                    break;
                case VALUE_TYPE.TIMER_FULL: //最终显示 计时器类型
                    str = ParseTimer(value, true);
                    break;
                case VALUE_TYPE.PERCENTAGE: //最终显示 百分比
                    str = Mathf.Round(value * 100).ToString() + '%';
                    break;
                case VALUE_TYPE.KMBT_FIXED2: //长单位缩放,只计算到 KMBT
                    if (value >= float.MaxValue)
                    {
                        str = "MAX";
                    }
                    else if (value >= 1000000000000000)
                    {
                        str = _regex_KMBT_FIXED2.Replace((value).ToString("E1"), "E");
                    }
                    else if (value >= 1000000000000)
                    {
                        str = (value / 1000000000000).ToString("F2") + 'T';
                    }
                    else if (value >= 1000000000)
                    {
                        str = (value / 1000000000).ToString("F2") + 'B';
                    }
                    else if (value >= 1000000)
                    {
                        str = (value / 1000000).ToString("F2") + 'M';
                    }
                    else if (value >= 9999)
                    {
                        str = (value / 1000).ToString("F2") + "K";
                    }
                    else
                    {
                        str = Mathf.RoundToInt(value).ToString();
                    }
                    break;
                case VALUE_TYPE.THOUSAND_SEPARATOR:
                    str = Mathf.RoundToInt(value).ToString("N0");
                    break;
                default:
                    break;
            }

            if (showSymbol)
            {
                if (value >= 0)
                {
                    str = "+" + str;
                }
            }

            //显示正负符号
            return str;
        }
    }


}